<template>
  <a-card :bordered="false" class="card-area">
    <div :class="advanced ? 'search' : null">
      <!-- 搜索区域 -->
      <a-form layout="horizontal">
        <div :class="advanced ? null: 'fold'">
          <a-row>
            <a-col :md="12" :sm="24">
              <a-form-item
                label="法人行"
                :labelCol="{span: 5}"
                :wrapperCol="{span: 16, offset: 1}">
                <a-select
                          mode="default"
                          :allowClear="true"
                          style="width: 100%"
                          @change="handleInstChange"
                          v-model="queryParams.instId">
                  <a-select-option v-for="r in instIdData" :key="r.label">{{r.label}}-{{r.value}}</a-select-option>
                </a-select>
<!--                <a-input v-if="!this.user.admin" v-model="queryParams.instId" :value="this.user.instId"
                         readonly="readonly"/>-->
              </a-form-item>
            </a-col>
            <a-col :md="12" :sm="24" v-show="queryParams.instId!=''">
            <a-form-item
              label="交易链路编号"
              :labelCol="{span: 5}"
              :wrapperCol="{span: 16, offset: 1}">
              <a-input placeholder="交易链路编号" v-model="queryParams.txnChainCode"/>
            </a-form-item>
          </a-col>
          </a-row>
          <a-row>
            <a-col :md="12" :sm="24" v-show="queryParams.instId!=''">
              <a-form-item
                label="交易链路描述"
                :labelCol="{span: 5}"
                :wrapperCol="{span: 16, offset: 1}">
                <a-input v-model="queryParams.txnChainDesc"/>
              </a-form-item>
            </a-col>
          </a-row>
        </div>
        <div v-show="queryParams.instId!=''" style="margin-left: 40%;">
          <!-- style=" margin-top: 3px; position: relative;left:-585px;top:45px;margin-top: 40px;" -->
          <a-button type="primary" @click="search" style="width: 60px;">查询</a-button>
          <a-button style="margin-left: 8px; width: 60px;" @click="reset">重置</a-button>
        </div>
      </a-form>
    </div>
    <div v-show="queryParams.instId!=''">
      <div class="operator">
        <a-button v-hasPermission="['reconPathInfo:add']" ghost type="primary" @click="add">新增</a-button>
<!--        <a-button v-hasPermission="['reconPathInfo:cancel']" ghost type="primary" @click="cancel">注销</a-button>-->
<!--        <a-button v-hasPermission="['reconPathInfo:stop']" ghost type="primary" @click="stopUse">停用</a-button>-->
<!--        <a-button v-hasPermission="['reconPathInfo:use']" ghost type="primary" @click="use">启用</a-button>-->

        <!--<a-popconfirm title="确定停用？" @confirm="reconSceneStop(record)" okText="确定" cancelText="取消">-->
        <!--<a-icon v-hasPermission="['reconscene:stop']" type="pause" style="color: #42b983" title="停用"/>&nbsp;&nbsp;-->
        <!--</a-popconfirm>&nbsp;&nbsp;-->
      </div>
      <!-- 表格区域 -->
      <a-table ref="TableInfo"
               :columns="columns"
               :rowKey="record => record.txnChainDesc"
               :dataSource="dataSource"
               :pagination="pagination"
               :loading="loading"
               :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
               :scroll="{ x: 2200 }"
               @change="handleTableChange">
        <template slot="email" slot-scope="text, record">
          <a-popover placement="topLeft">
            <template slot="content">
              <div>{{text}}</div>
            </template>
            <p style="width: 90px;margin-bottom: 0">{{text}}</p>
          </a-popover>
        </template>
        <template slot="reconPathOperation" slot-scope="text, record">
          <a-icon v-hasPermission="['reconPathInfo:update']" type="setting" theme="twoTone" twoToneColor="#4a9ff5"
                  @click="editReconPath(record)" title="修改"></a-icon>&nbsp;
          <a-icon v-hasPermission="['reconPathInfo:view']" type="eye" theme="twoTone" twoToneColor="#42b983"
                  @click="viewReconPath(record)" title="查看"></a-icon>
          <a-icon type="poweroff" v-hasPermission="['reconPathInfo:cancel']"
                  v-if="record.status !== '00' && record.status !== '03'"
                  @click="cancel(record)" title="注销"></a-icon>
          <a-icon type="check-circle" v-hasPermission="['reconPathInfo:update']" v-if="record.status == '02'"
                  @click="use(record)" theme="twoTone" twoToneColor="#4a9ff5" title="启用"></a-icon>
          <a-icon type="minus-circle" v-if="record.status == '01'" v-hasPermission="['reconPathInfo:update']"
                  @click="stopUse(record)" theme="twoTone" twoToneColor="#4a9ff5" title="停用"></a-icon>
          <a-badge v-hasNoPermission="['reconPathInfo:update','reconPathInfo:view']" userStatus="warning"
                   text="无权限"></a-badge>
        </template>

      </a-table>
      <!-- 查看 -->
      <ReconPathInfoInfo
        ref="reconPathInfoInfo"
        @close="handleReconPathInfoClose"
        :reconPathInfoInfoVisiable="reconPathInfoInfo.visiable"
        :reconPathCodeData="reconPathInfoInfo.data">

      </ReconPathInfoInfo>
      <!-- 新增 -->
      <ReconPathAdd
        @close="handleReconPathInfoAddClose"
        @success="handleReconPathInfoAddSuccess"
        :reconPathInfoAddVisable="reconPathInfoAdd.visiable"
        :reconPathInfoInstId="this.queryParams.instId"
        :reconPathCodeData="this.dataSource.select">
      </ReconPathAdd>

      <!-- 修改 -->
      <ReconPathEdit
        ref="reconPathEdit"
        @close="handleReconPathEditClose"
        @success="handleReconPathEditSuccess"
        :reconPathEditVisiable="reconPathEdit.visiable"
        :reconPathInfoInstId="this.queryParams.instId"
        :reconPathCodeData="this.dataSource.select">

      </ReconPathEdit>
    </div>
  </a-card>
</template>

<script>
  import RangeDate from '@/components/datetime/RangeDate'
  import ReconPathAdd from './ReconPathAdd'
  import ReconPathEdit from './ReconPathEdit'
  import {mapState} from "vuex"
  import ReconPathInfoInfo from "./ReconPathInfoInfo";

  export default {
    name: "ReconPath",
    components: {ReconPathInfoInfo, RangeDate, ReconPathAdd, ReconPathEdit},
    data() {
      return {
        advanced: false,
        reconPathInfoInfo: {
          visiable: false,
          data: {}
        },
        reconPathInfoAdd: {
          visiable: false
        },
        reconPathEdit: {
          visiable: false
        },
        queryParams: {
          txnChainCode: '',
          txnChainDesc: '',
          instId: ''
        },
        dataSource: [],
        sortedInfo: null,
        paginationInfo: null,
        selectedRowKeys: [],
        pagination: {
          pageSizeOptions: ['10', '20', '30', '40', '100'],
          defaultCurrent: 1,
          defaultPageSize: 10,
          showQuickJumper: true,
          showSizeChanger: true,
          showTotal: (total, range) => `显示 ${range[0]} ~ ${range[1]} 条记录，共 ${total} 条记录`
        },
        loading: false,
        instIdData: [],
      }
    },
    computed: {
      ...mapState({
        user: state => state.account.user,

      }),
      columns() {
        let {sortedInfo} = this
        sortedInfo = sortedInfo || {}
        let {filteredInfo} = this
        filteredInfo = filteredInfo || {}
        return [{
          title: '交易链路编号',
          dataIndex: 'txnChainCode',
          scopedSlots: {customRender: 'txnChainCode'}
        }, {
          title: '交易链路描述',
          dataIndex: 'txnChainDesc',
          scopedSlots: {customRender: 'txnChainDesc'},
        }, {
          title: '对账场景标识',
          dataIndex: 'reconSceneId',
          scopedSlots: {customRender: 'reconSceneId'},

        }, {
          title: '状态',
          dataIndex: 'status',
          customRender: (text, row, index) => {
            switch (text) {
              case '00':
                return '非正式'
              case '01':
                return '正式'
              case '02':
                return '停用'
              case '03':
                return '已注销'
              default:
                return text
            }
          },
        /*  filters: [
            {text: '非正式', value: '00'},
            {text: '正式', value: '01'},
            {text: '停用', value: '02'},
            {text: '已注销', value: '03'}
          ],
          filterMultiple: false,
          filteredValue: filteredInfo.status || null,
          onFilter: (value, record) => record.status.indexOf(value)===0*/
        }, {
          title: '审核状态',
          dataIndex: 'auditStatus',
          customRender: (text, row, index) => {
            switch (text) {
              case '00':
                return '待提交审批'
              case '01':
                return '新增-审批流程中'
              case '02':
                return '新增-审批通过'
              case '03':
                return '新增-审批拒绝'
              case '11':
                return '注销-审批流程中'
              case '12':
                return '注销-审批通过'
              case '13':
                return '注销-审批拒绝'
              case '21':
                return '停用-审批流程中'
              case '22':
                return '停用-审批通过'
              case '23':
                return '停用-审批拒绝'
              case '31':
                return '启用-审批流程中'
              case '32':
                return '启用-审批通过'
              case '33':
                return '启用-审批拒绝'
              case '41':
                return '修改-审批流程中'
              case '42':
                return '修改-审批通过'
              case '43':
                return '修改-审批拒绝'
              default:
                return text
            }
          },
          /*filters: [
            {text: '待提交审批', value: '00'},
            {text: '新增-审批流程中', value: '01'},
            {text: '新增-审批通过', value: '02'},
            {text: '新增-审批拒绝', value: '03'},
            {text: '注销-审批流程中', value: '11'},
            {text: '注销-审批通过', value: '12'},
            {text: '注销-审批拒绝', value: '13'},
            {text: '停用-审批流程中', value: '21'},
            {text: '停用-审批通过', value: '22'},
            {text: '停用-审批拒绝', value: '23'},
            {text: '启用-审批流程中', value: '31'},
            {text: '启用-审批通过', value: '32'},
            {text: '启用-审批拒绝', value: '33'},
            {text: '修改-审批流程中', value: '41'},
            {text: '修改-审批通过', value: '42'},
            {text: '修改-审批拒绝', value: '43'}
          ],
          filterMultiple: false,
          filteredValue: filteredInfo.auditStatus || null,
          onFilter: (value, record) => record.auditStatus.includes(value)*/

        }, {
          title: '创建时间',
          dataIndex: 'createTime',
          scopedSlots: {customRender: 'createTime'},
        }, {
          title: '审核时间',
          dataIndex: 'auditTime',
          scopedSlots: {customRender: 'auditTime'},
        }, {
          title: '操作',
          dataIndex: 'reconPathOperation',
          fixed: 'right',
          scopedSlots: {customRender: 'reconPathOperation'}
        }]
      }
    },
    mounted() {
      this.$get('admin/upmporg/getInstList').then((r) => {
        this.instIdData = r.data
      })
    },
    methods: {
      //注销
      // cancel() {
      //   if (this.selectedRowKeys.length === 0) {
      //     this.$message.warning("请选择一条数据")
      //     return;
      //   }
      //   let selectedIndex = ''
      //   this.dataSource.map((val, index) => {
      //     if (val.txnChainDesc === this.selectedRowKeys[0]) return selectedIndex = index
      //   })
      //   let param = this.dataSource[selectedIndex]
      //
      //   if (param.status != '01' && param.status != '02') {
      //     this.$message.warning("非正式、已注销状态不能注销")
      //     return;
      //   } else if (param.auditStatus === '01' || param.auditStatus === '11' || param.auditStatus === '21'
      //     || param.auditStatus === '31' || param.auditStatus === '41') {
      //     this.$message.warning('审核流程中的数据不允许注销')
      //     return;
      //   } else if (param.auditStatus === '03') {
      //     this.$message.warning('注销通过的数据不允许再注销')
      //     return;
      //   } else {
      //     param.instId = this.queryParams.instId;
      //     param.flag = "cancel";
      //     this.$put('/admin/csmc/recon-path-info/check', param).then((r) => {
      //       this.selectedRowKeys = []
      //       this.search()
      //       this.$message.success('注销成功！')
      //     }).catch(() => {
      //       this.loading = false
      //     })
      //   }
      // },

      //将按钮移入操作后的注销方法
      cancel(record) {
        console.log(record);
        if (record.status != '01' && record.status != '02') {
          this.$message.warning("非正式、已注销状态不能注销")
          return;
        } else if (record.auditStatus === '01' || record.auditStatus === '11' || record.auditStatus === '21'
          || record.auditStatus === '31' || record.auditStatus === '41') {
          this.$message.warning('审核流程中的数据不允许注销')
          return;
        } else if (record.auditStatus === '03') {
          this.$message.warning('注销通过的数据不允许再注销')
          return;
        } else {
          record.instId = this.queryParams.instId;
          record.flag = "cancel";
          this.$put('/admin/csmc/recon-path-info/check', record).then((r) => {
            this.selectedRowKeys = []
            this.search()
            this.$message.success('注销成功！')
          }).catch(() => {
            this.loading = false
          })
        }
      },
      //停用
      // stopUse() {
      //   if (this.selectedRowKeys.length === 0) {
      //     this.$message.warning("请选择一条数据")
      //     return;
      //   }
      //   let selectedIndex = ''
      //   this.dataSource.map((val, index) => {
      //     if (val.txnChainDesc === this.selectedRowKeys[0]) return selectedIndex = index
      //   })
      //   let param = this.dataSource[selectedIndex]
      //   if (param.status != '01') {
      //     this.$message.warning("未审核、停用、注销状态时不能停用")
      //     return;
      //   } else if (param.auditStatus === '01' || param.auditStatus === '11' || param.auditStatus === '21'
      //     || param.auditStatus === '31' || param.auditStatus === '41') {
      //     this.$message.warning('审核流程中的数据不允许停用')
      //     return;
      //   } else if (param.auditStatus === '22') {
      //     this.$message.warning("停用的数据不允许再停用")
      //     return;
      //   } else {
      //     param.instId = this.queryParams.instId;
      //     param.flag = "stop";
      //     this.$put('/admin/csmc/recon-path-info/check', param).then((r) => {
      //       this.selectedRowKeys = []
      //       this.search()
      //       this.$message.success('停用成功！')
      //     }).catch(() => {
      //       this.loading = false
      //     })
      //   }
      // },

      //将按钮移入操作的停用方法
      stopUse(record) {
        if (record.status != '01') {
          this.$message.warning("未审核、停用、注销状态时不能停用")
          return;
        } else if (record.auditStatus === '01' || record.auditStatus === '11' || record.auditStatus === '21'
          || record.auditStatus === '31' || record.auditStatus === '41') {
          this.$message.warning('审核流程中的数据不允许停用')
          return;
        } else if (record.auditStatus === '22') {
          this.$message.warning("停用的数据不允许再停用")
          return;
        } else {
          record.instId = this.queryParams.instId;
          record.flag = "stop";
          this.$put('/admin/csmc/recon-path-info/check', record).then((r) => {
            this.selectedRowKeys = []
            this.search()
            this.$message.success('停用成功！')
          }).catch(() => {
            this.loading = false
          })
        }
      },
      //启用
      // use() {
      //   if (this.selectedRowKeys.length === 0) {
      //     this.$message.warning("请选择一条数据")
      //     return;
      //   }
      //   let selectedIndex = ''
      //   this.dataSource.map((val, index) => {
      //     if (val.txnChainDesc === this.selectedRowKeys[0]) return selectedIndex = index
      //   })
      //   let param = this.dataSource[selectedIndex]
      //
      //   if (param.status != '02') {
      //     this.$message.warning("未审核、正式、注销状态不能启用")
      //     return;
      //   } else if (param.auditStatus === '01' || param.auditStatus === '11' || param.auditStatus === '21'
      //     || param.auditStatus === '31' || param.auditStatus === '41') {
      //     this.$message.warning('审核流程中的数据不允许停用')
      //     return;
      //   } else if (param.auditStatus === '32') {
      //     this.$message.warning("已启用的数据不需再启用")
      //     return;
      //   } else {
      //     param.instId = this.queryParams.instId;
      //     param.flag = "use";
      //     this.$put('/admin/csmc/recon-path-info/check', param).then((r) => {
      //       this.selectedRowKeys = []
      //       this.search()
      //       this.$message.success('启用成功！')
      //     }).catch(() => {
      //       this.loading = false
      //     })
      //   }
      // },

      //将按钮移入操作后的启用方法
      use(record) {
        if (record.status != '02') {
          this.$message.warning("未审核、正式、注销状态不能启用")
          return;
        } else if (record.auditStatus === '01' || record.auditStatus === '11' || record.auditStatus === '21'
          || record.auditStatus === '31' || record.auditStatus === '41') {
          this.$message.warning('审核流程中的数据不允许停用')
          return;
        } else if (record.auditStatus === '32') {
          this.$message.warning("已启用的数据不需再启用")
          return;
        } else {
          record.instId = this.queryParams.instId;
          record.flag = "use";
          this.$put('/admin/csmc/recon-path-info/check', record).then((r) => {
            this.selectedRowKeys = []
            this.search()
            this.$message.success('启用成功！')
          }).catch(() => {
            this.loading = false
          })
        }
      },
      onSelectChange(selectedRowKeys) {   //接收数组，存放到前面定义的数组中
        this.selectedRowKeys = selectedRowKeys
      },

      add() {
        this.reconPathInfoAdd.visiable = true
      },

      handleInstChange(value) {
        this.queryParams.instId = value;
        if (value != "") {
          let params = {}
          params.instId = value;
          this.$get('/admin/csmc/recon-path-info', {...params}).then((r) => {

            this.dataSource = r.data.rows;
            this.search();
          })
        }
      },

      handleReconPathInfoAddClose() {
        this.reconPathInfoAdd.visiable = false
      },

      handleReconPathInfoAddSuccess() {
        this.reconPathInfoAdd.visiable = false
        this.$message.success('新增成功')
        this.search()
      },

      handleBrandImportClose() {
        this.brandUpload.visiable = false
        this.search()
      },

      viewReconPath(record) {
        this.reconPathInfoInfo.data = record
        this.$refs.reconPathInfoInfo.setReconPathFormValues(record)
        this.reconPathInfoInfo.visiable = true
      },

      handleReconPathInfoClose() {
        this.reconPathInfoInfo.visiable = false
      },

      editReconPath(record) {
        if (record.auditStatus === '01' || record.auditStatus === '11' || record.auditStatus === '21'
          || record.auditStatus === '31' || record.auditStatus === '41') {
          this.$confirm({
            title: '提示',
            content: '申请待审批的数据不允许修改',
          });
        } else if (record.status === '02' || record.status === '03') {
          this.$confirm({
            title: '提示',
            content: '停用、注销状态时不能修改',
          });
        } else {
          this.$refs.reconPathEdit.setReconPathFormValues(record)
          this.reconPathEdit.visiable = true
        }
      },

      handleReconPathEditSuccess() {
        this.reconPathEdit.visiable = false
        this.$message.success('修改品牌成功')
        this.search()
      },

      handleReconPathEditClose() {
        this.reconPathEdit.visiable = false
      },


      search() {
        let {sortedInfo, filteredInfo} = this
        let sortField, sortOrder
        // 获取当前列的排序和列的过滤规则
        if (sortedInfo) {
          sortField = sortedInfo.field
          sortOrder = sortedInfo.order
        }
        let params = {}
        params.instId = this.queryParams.instId
        params.txnChainCode = this.queryParams.txnChainCode
        params.txnChainDesc = this.queryParams.txnChainDesc

        this.fetch({
          sortField: sortField,
          sortOrder: sortOrder,
          ...params,
          ...filteredInfo
        })
      },

      reset() {
        // 取消选中
        this.selectedRowKeys = []
        // 重置分页
        this.$refs.TableInfo.pagination.current = this.pagination.defaultCurrent
        if (this.paginationInfo) {
          this.paginationInfo.current = this.pagination.defaultCurrent
          this.paginationInfo.pageSize = this.pagination.defaultPageSize
        }
        // 重置列排序规则
        this.sortedInfo = null
        // 重置查询参数
        this.queryParams = {}
        // // 清空时间选择
        // this.$refs.createTime.reset()
        this.fetch()
      },

      handleTableChange(pagination, filters, sorter) {
        // 将这三个参数赋值给Vue data，用于后续使用
        this.paginationInfo = pagination
        this.filteredInfo = filters
        this.sortedInfo = sorter
        // this.userInfo.visiable = false

        this.fetch({
          sortField: sorter.field,
          sortOrder: sorter.order,
          ...this.queryParams,
          ...filters
        })
      },

      fetch(params = {}) {
        this.loading = true
        if (this.paginationInfo) {
          // 如果分页信息不为空，则设置表格当前第几页，每页条数，并设置查询分页参数
          this.$refs.TableInfo.pagination.current = this.paginationInfo.current
          this.$refs.TableInfo.pagination.pageSize = this.paginationInfo.pageSize
          params.pageSize = this.paginationInfo.pageSize
          params.pageNum = this.paginationInfo.current
        } else {
          // 如果分页信息为空，则设置为默认值
          params.pageSize = this.pagination.defaultPageSize
          params.pageNum = this.pagination.defaultCurrent
        }
        this.$get('/admin/csmc/recon-path-info', {
          ...params
        }).then((r) => {
          let data = r.data
          const pagination = {...this.pagination}
          pagination.total = data.total
          this.dataSource = data.rows
          this.pagination = pagination
          this.loading = false
        })
      }
    }
  }
</script>

<style lang="less" scoped>
  @import "../../../../../static/less/Common";
</style>
